package com.example.hyw.szysteam.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.hyw.szysteam.POJO.Practice;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.adapter.PracticeAdapter;
import com.example.hyw.szysteam.presenter.CreateHomeWorkPresenter;
import com.example.hyw.szysteam.utils.ShowTips;
import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by HYW on 2018/6/14.
 */

public class CreateHomeWorkActivity extends AppCompatActivity {
    EditText etQuestion;
    EditText etAnswer;
    ListView listView;
    String classid;
    ArrayList<Practice> practices=new ArrayList<Practice>();
    Date date;
    private PracticeAdapter practiceAdapter;
    QMUIRoundButton add;
    QMUITopBar mTopBar;
    CreateHomeWorkPresenter createHomeWorkPresenter=new CreateHomeWorkPresenter(this);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createhomework);
        Bundle bundle=getIntent().getExtras();
        classid=bundle.getString("classid");
        initTopbar();
        initView();
    }

    private void initTopbar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.setTitle("发布作业").setTextColor(getResources().getColor(R.color.myWhite));
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTopBar.addRightTextButton("发布",R.id.right).setTextColor(getResources().getColor(R.color.myWhite));
        View right=findViewById(R.id.right);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final QMUIDialog.EditTextDialogBuilder builder=new QMUIDialog.EditTextDialogBuilder(CreateHomeWorkActivity.this);
                builder.setTitle("作业名称")
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text=builder.getEditText().getText();
                                if (text!=null && text.length()>0) {
                                    date = new Date(System.currentTimeMillis());
                                    Gson gson=new Gson();
                                    String tmp=gson.toJson(practices);
                                    createHomeWorkPresenter.createHomeWork(classid,text.toString(),date.toString(),tmp);
                                }
                            }
                        })
                        .show();
            }
        });
    }

    private void initView(){
        listView=findViewById(R.id.lv_practice);
        add=findViewById(R.id.add);
        etQuestion=findViewById(R.id.et_question);
        etAnswer=findViewById(R.id.et_answer);
        practiceAdapter =new PracticeAdapter(CreateHomeWorkActivity.this,R.layout.item_practice,practices);
        listView.setAdapter(practiceAdapter);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String question=etQuestion.getText().toString();
                String answer=etAnswer.getText().toString();
                if (!question.equals("") && !answer.equals("")){
                    Practice practice=new Practice();
                    practice.setQuestion(question);
                    practice.setAnswer(answer);
                    practices.add(practice);
                    practiceAdapter.notifyDataSetChanged();
                    listView.setSelection(practices.size());
                    etQuestion.setText("");
                    etAnswer.setText("");
                }else {
                    Toast.makeText(getApplicationContext(),"请输入题目和答案",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void success(){
        final QMUITipDialog qmuiTipDialog;
        qmuiTipDialog=new QMUITipDialog.Builder(CreateHomeWorkActivity.this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("发布成功")
                .create();
        qmuiTipDialog.show();
        listView.postDelayed(new Runnable() {
            @Override
            public void run() {
                qmuiTipDialog.dismiss();
                finish();
            }
        },1000);
    }
}
